package com.example.shellshopping.personCenter;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.shellshopping.MainActivity;
import com.example.shellshopping.R;
import com.example.shellshopping.YCB_settings.YCBAboutActivity;
import com.example.shellshopping.YCB_settings.YCBSettingsAnquanActivity;
import com.example.shellshopping.YCB_settings.YCBSettingsTongyongActivity;
import com.example.shellshopping.YCB_settings.YCBSettingsXiaoxiActivity;
import com.example.shellshopping.YCB_settings.YCBSettingsZhifuActivity;
import com.example.shellshopping.login.PersonCenterMainActivity;
import com.example.shellshopping.login.UserBean;
import com.example.shellshopping.utils.BaseActivity;
import com.example.shellshopping.utils.SaveUserUtils;
import com.example.shellshopping.utils.URLUtils;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class PersonCenterSettingActivity extends BaseActivity {
    ImageView backTV;
    Button repwdTV,btn8,btn4,btn1,btn3,btn5,btn6,btn7,btn9;
    TextView questionTV;
    static String str;

    private UserBean.DataBean userInfo; //之前登录时保存的用户信息
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_person_center_setting);
        userInfo = SaveUserUtils.getUserInfo(this);
        initView();
        setListener();
    }

    private void setListener() {
        repwdTV.setOnClickListener(onClickListener);
        backTV.setOnClickListener(onClickListener);
        btn8.setOnClickListener(onClickListener);
        btn1.setOnClickListener(onClickListener);
        btn3.setOnClickListener(onClickListener);
        btn4.setOnClickListener(onClickListener);
        btn5.setOnClickListener(onClickListener);
        btn6.setOnClickListener(onClickListener);
        //btn7.setOnClickListener(onClickListener);
        btn9.setOnClickListener(onClickListener);
    }



    View.OnClickListener onClickListener = new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            int id = v.getId();
            Intent intent;
            switch (id){
                case R.id.center_setting_iv2:
                    //jumpToRepwd();  //跳转到修改密码界面
                    getQuestion();
                    break;
                case R.id.center_setting_iv_back:
                    //jumpToRepwd();
                    finish();
                    break;
                case R.id.activity_person_center_setting_btn1:
                    break;
                case R.id.activity_person_center_setting_btn3:
                    intent = new Intent(PersonCenterSettingActivity.this, YCBSettingsAnquanActivity.class);
                    startActivity(intent);
                    break;
                case R.id.activity_person_center_setting_btn4:
                    intent = new Intent(PersonCenterSettingActivity.this, YCBSettingsZhifuActivity.class);
                    startActivity(intent);
                    break;
                case R.id.activity_person_center_setting_btn5:
                    intent = new Intent(PersonCenterSettingActivity.this, YCBSettingsXiaoxiActivity.class);
                    startActivity(intent);
                    break;
                case R.id.activity_person_center_setting_btn6:
                    intent = new Intent(PersonCenterSettingActivity.this, YCBSettingsTongyongActivity.class);
                    startActivity(intent);
                    break;
                //case R.id.activity_person_center_setting_btn7:
                    //break;
                case R.id.activity_person_center_setting_btn8:
                    intent = new Intent(PersonCenterSettingActivity.this, YCBAboutActivity.class);
                    startActivity(intent);
                    break;
                case R.id.activity_person_center_setting_btn9:
                    exitLogin();
                    break;
                default:
                    break;
            }
        }
    };

    //  退出登录，注销
    private void exitLogin() {
        UserBean.DataBean userInfo = SaveUserUtils.getUserInfo(this);
        if (userInfo == null) {
            Toast.makeText(this, "您还未登录账号", Toast.LENGTH_SHORT).show();
        }else {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示信息").setMessage("您是否退出登录？")
                    .setPositiveButton("取消",null)
                    .setNegativeButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            //删除文件中的内容
                            SaveUserUtils.deleteUserInfo(PersonCenterSettingActivity.this);
                            //logintv.setText("未登录");
                            Toast.makeText(PersonCenterSettingActivity.this, "注销成功", Toast.LENGTH_SHORT).show();
                            //MeFragment mefrag = (MeFragment)fragmentList.get(2);
                            //mefrag.setHeadview;
                        }
                    });
            builder.show();
        }
    }

    private void getQuestion() {
        Map<String, String> map = new HashMap<>();
        int i = userInfo.getId();
        map.put("id", String.valueOf(userInfo.getId()));
        getNetwork(URLUtils.showQuestion_url,map);
    }

    private void jumpToRepwd() {
        Intent intent = new Intent(this, PersonSettingQuestionActivity.class);
        startActivity(intent);
    }

    private void jumpToCenter() {
        finish();
        Intent intent = new Intent(this, PersonCenterMainActivity.class);
        startActivity(intent);
    }

    @Override
    public void onSuccess(String result) {
        super.onSuccess(result);
        //解析数据
        UserBean bean = new Gson().fromJson(result, UserBean.class);
        //
        int code = bean.getCode();
        switch(code){
            case 200:  //登录成功
                LayoutInflater inflater = LayoutInflater.from(this);
                View myview = inflater.inflate(R.layout.activity_person_setting_question, null);//引用自定义布局
                questionTV = myview.findViewById(R.id.person_setting_question_tv);
                //questionTV.setText(bean.getMessage());
                str = bean.getMessage();
                //questionTV.setText("bean.getMessage()");
                //Toast.makeText(this,"获取密保问题成功！",Toast.LENGTH_SHORT).show();
                //jumpToRepwd();  //跳转到修改密码界面
                break;
            case 201:  //账号或密码错误
                Toast.makeText(this,"获取密保问题失败！",Toast.LENGTH_SHORT).show();
                jumpToRepwd();  //跳转到修改密码界面
                break;
            default:   //其他，登录失败
                jumpToRepwd();  //跳转到修改密码界面
                Toast.makeText(this,"获取异常！",Toast.LENGTH_SHORT).show();
                break;
        }
    }


    private void initView() {
        repwdTV = findViewById(R.id.center_setting_iv2);
        backTV = findViewById(R.id.center_setting_iv_back);
        btn8 = findViewById(R.id.activity_person_center_setting_btn8);
        btn4 = findViewById(R.id.activity_person_center_setting_btn4);
        btn1 = findViewById(R.id.activity_person_center_setting_btn1);
        btn3 = findViewById(R.id.activity_person_center_setting_btn3);
        btn5 = findViewById(R.id.activity_person_center_setting_btn5);
        btn6 = findViewById(R.id.activity_person_center_setting_btn6);
        //btn7 = findViewById(R.id.activity_person_center_setting_btn7);
        btn9 = findViewById(R.id.activity_person_center_setting_btn9);
    }
}